import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 数据可视化
import * as echarts from "echarts";
// 引入element的按需引入文件
import './plugins/element.js'
// 引入全局样式
import './assets/css/global.css'
// axios引入
// 引入包
import axios from "axios"
import ElementUI from 'element-ui';
// 动画包
import animated from 'animate.css'
Vue.use(animated)
// 阻止生产警告提示
Vue.config.productionTip = false
// 声明对象
Vue.prototype.$echarts = echarts;
// 配置请求根路径
axios.defaults.baseURL = 'http://127.0.0.1:88'
//设置cross跨域 并设置访问权限 允许跨域携带cookie信息
axios.defaults.withCredentials=true;
// 将axios挂载到$http 的隐式对象上
Vue.prototype.$http = axios
// 除去登录其他需要鉴权
axios.interceptors.request.use(config => {
  if (config.push === '/' || config.push === '/login') {
    
  }else {
    if (localStorage.getItem('token')) {
      console.log("==========携带token==========");
      config.headers.token = localStorage.getItem('token');
    }
  }
  return config;
},
error => {
  return Promise.reject(error);
});
// 响应结果
axios.interceptors.response.use(response => {
  console.log("==========响应token认证==========");
  return response;
},
error => {
  if (error.response.status == 401) {
    console.log("==========响应token过期==========");
    ElementUI.Message({
      message:'身份已失效',
      type:'error'    
    })
    localStorage.removeItem('token');
    localStorage.removeItem('userName');
    // 跳转
    router.push({
      name:'login'
    })
    console.log("==========响应token过期==========");
  }
  return Promise.reject(error);
});
// 创建Vue对象
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')